package com.tao.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tao.pojo.Answerer;
import com.tao.pojo.Questioner;
import com.tao.service.QuestionerService;
import com.tao.mapper.QuestionerMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
@CacheConfig(cacheNames = "QuestionTable")
public class QuestionerServiceImpl extends ServiceImpl<QuestionerMapper, Questioner>
    implements QuestionerService{

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    QuestionerMapper questionerMapper;

    @Override
    @Cacheable(key ="'Quetioner::ByName.'+#p0")
    public Questioner selectByName(String username) {
        QueryWrapper<Questioner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",username);
        return  questionerMapper.selectOne(queryWrapper);
    }

    @Override
    @Cacheable(key ="'Quetioner::All'")
    public List<Questioner> selectList() {
        QueryWrapper<Questioner> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(Questioner.class, info -> !info.getColumn().equals("password"));
        return   questionerMapper.selectList(queryWrapper);
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(key ="'Quetioner::all'"),
                    @CacheEvict(key ="'Quetioner::ByName.'+#p0.name"),
            }
    )
    public boolean update(Questioner entity, Wrapper<Questioner> updateWrapper) {
        return super.update(entity, updateWrapper);
    }
}




